package starComponent.controls.rules;

import starComponent.E_PossibleBranches;
import starComponent.controls.StarComponentControler;
import starComponent.elements.A_StarBranch;
import starComponent.elements.StarBranchSlider;

/**
 * @author Charles Perin
 * 21 avr. 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 */
public class RuleNbKmAndGazol extends RuleTwoParams{
	
	public static final int NB_KM_PRIORITY = 0;
	public static final int GAZOL_PRIORITY = 1;

	private StarBranchSlider nbKm;
	private StarBranchSlider gazol;

	private boolean isGazolRestricting;
	private double gazolMaxAutorised, gazolMinAutorised;

	public RuleNbKmAndGazol(StarComponentControler starControler, int priority, int weight) {
		super(starControler, E_PossibleBranches.NB_KM, E_PossibleBranches.GAZOL, priority, weight);
	}

	@Override
	public void initBranches() {
		nbKm = (StarBranchSlider) branch1;
		gazol = (StarBranchSlider) branch2;
	}

	private void limitGazolRange() {
		isGazolRestricting = true;
		gazolMinAutorised = nbKm.getValue()/8;
		gazolMaxAutorised = gazol.getMax();
	}

	@Override
	public int modifyParam(A_StarBranch branch) {
		setGazolRestricting(false);
		if(branch == nbKm){
			if(checkNbKmPriorityDependency())return MODIFYING_RULE;
			return NOTHING_RULE;
		}
		else if(branch == gazol){
			limitGazolRange();
			if(checkNbKmPriorityDependency()){
				return BLOCKING_RULE;
			}
			else return INFORMATIVE_RULE;
		}
		else{
			System.err.println("RuleShotDurationAndPacing: modifyParam: branche impossible");
			return NOTHING_RULE;
		}
	}


	private boolean checkNbKmPriorityDependency(){
		if(gazol.getValue()<=nbKm.getValue()/8){
			gazol.getThumb().setBlockedValue(nbKm.getValue()/8);
			return true;
		}
		return false;
	}

	public void setGazolRestricting(boolean isgazolRestricting) {
		this.isGazolRestricting = isgazolRestricting;
	}

	public boolean isGazolRestricting() {
		return isGazolRestricting;
	}

	public void setGazolMaxAutorised(double gazolMaxAutorised) {
		this.gazolMaxAutorised = gazolMaxAutorised;
	}

	public double getGazolMaxAutorised() {
		return gazolMaxAutorised;
	}

	public void setGazolMinAutorised(double gazolMinAutorised) {
		this.gazolMinAutorised = gazolMinAutorised;
	}

	public double getGazolMinAutorised() {
		return gazolMinAutorised;
	}
}
